package com.iticit.api.send.service;


import com.alibaba.fastjson.JSONObject;
import com.iticit.api.vo.bescanned.CopsBeScannedReqVo;
import com.iticit.api.vo.bescanned.CopsBeScannedResVo;
import com.iticit.api.vo.closeorder.CopsCloseOrderReqVo;
import com.iticit.api.vo.closeorder.CopsCloseOrderResVo;
import com.iticit.api.vo.openid.CopsOpenIdFetchReqVo;
import com.iticit.api.vo.openid.CopsOpenIdFetchResVo;
import com.iticit.api.vo.payquery.CopsPayOrderQueryReqVo;
import com.iticit.api.vo.payquery.CopsPayOrderQueryResVo;
import com.iticit.api.vo.preorder.CopsPreOrderReqVo;
import com.iticit.api.vo.preorder.CopsPreOrderResVo;
import com.iticit.api.vo.refund.CopsRefundReqVo;
import com.iticit.api.vo.refund.CopsRefundResVo;
import com.iticit.api.vo.refundquery.CopsRefundQueryReqVo;
import com.iticit.api.vo.refundquery.CopsRefundQueryResVo;
import com.iticit.api.vo.wx.minipro.CopsMiniProReqVo;
import com.iticit.api.vo.wx.minipro.CopsMiniProResVo;
import com.iticit.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;



/**
 *
 * @description
 * <p>发送报文到金融开放平台</p>
 * @author chenchengdong-yfzx
 * @date 2019年12月5日
 */
@Slf4j
@Service
public class CopsService extends CopsBaseService {


	/**
	 * 微信公众号/小程序预下单
	 * @param copsMiniProReqVo
	 */
	public CopsMiniProResVo miniProSend(CopsMiniProReqVo copsMiniProReqVo) {
		log.info("copsMiniProReqVo---->{}", JSONObject.toJSONString(copsMiniProReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsMiniProReqVo);
			log.info("the requestJsonReport of miniPro is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getWechatMiniPro(),copsMiniProReqVo.getBaseRequestUrl());
			log.info("the responseJsonReport of miniPro is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsMiniProResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}

	/**
	 * 退款
	 * @param copsRefundReqVo
	 */
	public CopsRefundResVo refundSend(CopsRefundReqVo copsRefundReqVo) {
		log.info("copsRefundReqVo---->{}", JSONObject.toJSONString(copsRefundReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsRefundReqVo);
			log.info("the requestJsonReport of refund is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getRefund(),null);
			log.info("the responseJsonReport of refund is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsRefundResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}

	/**
	 * 退款订单查询
	 * @param copsRefundQueryReqVo
	 */
	public CopsRefundQueryResVo refundQuerySend(CopsRefundQueryReqVo copsRefundQueryReqVo) {
		log.info("copsRefundQueryReqVo---->{}", JSONObject.toJSONString(copsRefundQueryReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsRefundQueryReqVo);
			log.info("the requestJsonReport of refundQuery is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getRefundQuery(),null);
			log.info("the responseJsonReport of refundQuery is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsRefundQueryResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}

	/**
	 * 支付订单查询
	 * @param copsPayOrderQueryReqVo
	 */
	public CopsPayOrderQueryResVo orderQuerySend(CopsPayOrderQueryReqVo copsPayOrderQueryReqVo) {
		log.info("copsPayOrderQueryReqVo---->{}", JSONObject.toJSONString(copsPayOrderQueryReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsPayOrderQueryReqVo);
			log.info("the requestJsonReport of orderQuery is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getPayQuery(),copsPayOrderQueryReqVo.getBaseRequestUrl());
			log.info("the responseJsonReport of orderQuery is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsPayOrderQueryResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}


	/**
	 * openId获取
	 * @param copsOpenIdFetchReqVo
	 */
	public CopsOpenIdFetchResVo openIdFetchSend(CopsOpenIdFetchReqVo copsOpenIdFetchReqVo) {
		log.info("copsOpenIdFetchReqVo---->{}", JSONObject.toJSONString(copsOpenIdFetchReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsOpenIdFetchReqVo);
			log.info("the requestJsonReport of openIdFetch is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getOpenIdFetch(),null);
			log.info("the responseJsonReport of openIdFetch is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsOpenIdFetchResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}


	/**
	 * 付款码支付
	 * @param copsBeScannedReqVo
	 */
	public CopsBeScannedResVo beScannedSend(CopsBeScannedReqVo copsBeScannedReqVo) {
		log.info("copsBeScannedReqVo---->{}", JSONObject.toJSONString(copsBeScannedReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(copsBeScannedReqVo);
			log.info("the requestJsonReport of beScanned is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getBeScanned(),null);
			log.info("the responseJsonReport of beScanned is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsBeScannedResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}

	/**
	 * 关单
	 * @param closeOrderReqVo
	 */
	public CopsCloseOrderResVo closeOrderSend(CopsCloseOrderReqVo closeOrderReqVo) {
		log.info("closeOrderReqVo---->{}", JSONObject.toJSONString(closeOrderReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(closeOrderReqVo);
			log.info("the requestJsonReport of closeOrder is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getCloseOrder(),null);
			log.info("the responseJsonReport of closeOrder is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsCloseOrderResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}

	/**
	 * 统一预下单
	 * @param preOrderReqVo
	 */
	public CopsPreOrderResVo preOrderSend(CopsPreOrderReqVo preOrderReqVo) {
		log.info("preOrderReqVo---->{}", JSONObject.toJSONString(preOrderReqVo));
		try {
			// 请求报文
			String requestJsonReport = JSONObject.toJSONString(preOrderReqVo);
			log.info("the requestJsonReport of preOrder is --->{}", requestJsonReport);
			// 发送并返回响应报文
			String responseJsonReport = send(requestJsonReport, copsPropertyConfig.getPreOrder(),null);
			log.info("the responseJsonReport of preOrder is --->{}", responseJsonReport);
		    return JSONObject.parseObject(responseJsonReport, CopsPreOrderResVo.class);
		} catch (BusinessException e) {
			log.error("there is a businessException ---> {}", e);
		} catch (Exception e) {
			log.error("there is an exception ---> {}", e);
		}
		return null;
	}
}
